/**
 * 
 */
package com.suneee.spec.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.suneee.context.page.Page;
import com.suneee.context.page.Sort;
import com.suneee.context.page.Sort.Order;
import com.suneee.core.util.StringUtils;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.spec.bean.SpecSearchBean;
import com.suneee.spec.dao.SpecDao;
import com.suneee.spec.entity.Spec;

/**
 * @author liuxh
 *
 */
@Repository("specDao")
public class SpecDaoImpl extends GenericDao<Spec, Integer>implements SpecDao{

	@Override
	public boolean existsName(String name,String categoryCode) {
		Object[] args = new Object[2];
		args[0] = name;
		args[1] = categoryCode;
		List<Spec> list = this.selectWhere(" name = ? and category_code = ?",args);
		return list != null && list.size() > 0;
	}

	@Override
	public Page<Spec> findSpec(SpecSearchBean bean) {
		if (bean == null) {
			bean = new SpecSearchBean();
		}
		WhereClauseBuilder builder = new WhereClauseBuilder();
		builder.contains("name",bean.getName());
		bean.setPageNumber(bean.getPageNumber() == 0 ? 1 : bean.getPageNumber());
		bean.setPageSize(bean.getPageSize() == 0 ? 10 : bean.getPageSize());
		bean.setSort((bean.getSort() == null || StringUtils.isEmpty(bean.getSort().getOrderByClause()))
				? new Sort("order_number", Order.ASC) : bean.getSort());
		return selectWhere(builder.getSql(), bean, builder.getArgs());
	}

	@Override
	public List<Spec> listSpec(String name) {
		WhereClauseBuilder wcb = new WhereClauseBuilder();
		wcb.contains("name",name);
		return this.selectWhere(wcb.getSql(), new Sort("order_number", Order.ASC),wcb.getArgs());
	}

	@Override
	public List<Spec> getSpecByCategoryCode(String categoryCode) {
		// TODO Auto-generated method stub
		return this.selectWhere(" category_code=? ", categoryCode);
	}
	

}
